package com.Xht.www.servlet;

//后端删除房型的请求

import com.Xht.www.dao.RoomTypeInfoDAO;
import com.Xht.www.po.RoomTypeInfoPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/ManageRoomServlet")
public class ManageRoomServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        String merchantName = request.getParameter("merchantName");
        RoomTypeInfoDAO roomTypeInfoDAO = new RoomTypeInfoDAO();

        if ("showModifyForms".equals(action)) {
            response.setContentType("application/json;charset=UTF-8");
            try {
                List<RoomTypeInfoPO> roomTypeInfoList = roomTypeInfoDAO.queryRoomTypeInfoByMerchantName(merchantName);
                StringBuilder json = new StringBuilder("[");
                for (int i = 0; i < roomTypeInfoList.size(); i++) {
                    RoomTypeInfoPO roomTypeInfo = roomTypeInfoList.get(i);
                    json.append("{");
                    json.append("\"roomTypeId\":").append(roomTypeInfo.getRoomTypeId()).append(",");
                    json.append("\"merchantName\":\"").append(roomTypeInfo.getMerchantName()).append("\",");
                    json.append("\"bedType\":\"").append(roomTypeInfo.getBedType()).append("\",");
                    json.append("\"remainingQuantity\":").append(roomTypeInfo.getRemainingQuantity()).append(",");
                    json.append("\"keywords\":\"").append(roomTypeInfo.getKeywords()).append("\",");
                    json.append("\"price\":").append(roomTypeInfo.getPrice());
                    json.append("}");
                    if (i < roomTypeInfoList.size() - 1) {
                        json.append(",");
                    }
                }
                json.append("]");
                response.getWriter().println(json.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // 原有的查询和删除逻辑
            PrintWriter out = response.getWriter();
            if ("delete".equals(action)) {
                int roomTypeId = Integer.parseInt(request.getParameter("roomTypeId"));
                try {
                    roomTypeInfoDAO.deleteRoomTypeInfo(roomTypeId);
                    out.println("房型信息删除成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    out.println("房型信息删除失败");
                }
            } else {
                try {
                    List<RoomTypeInfoPO> roomTypeInfoList = roomTypeInfoDAO.queryRoomTypeInfoByMerchantName(merchantName);
                    for (RoomTypeInfoPO roomTypeInfo : roomTypeInfoList) {
                        out.println("房型ID: " + roomTypeInfo.getRoomTypeId());
                        out.println("商家名称: " + roomTypeInfo.getMerchantName());
                        out.println("床型: " + roomTypeInfo.getBedType());
                        out.println("剩余数量: " + roomTypeInfo.getRemainingQuantity());
                        out.println("关键词: " + roomTypeInfo.getKeywords());
                        out.println("价格: " + roomTypeInfo.getPrice());
                        out.println("销量: " + roomTypeInfo.getSalesVolume());
                        out.println("<button onclick=\"confirmDelete(" + roomTypeInfo.getRoomTypeId() + ", '" + merchantName + "')\">删除</button>");
                        out.println("<hr>");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    out.println("查询房型信息失败");
                }
            }
        }
    }
}